import math
from math import sin, cos
import networkx as nx
import re
import sys
from random import randint
import random
from NewsMining.db.dbConnect import getDBConnection
from mynewsweb.model import *
import testData

kernel_id = 1
threshold = 0.2
data_limit = 500


db = getDBConnection()

##pozitive_news = db.query(News.news_id, News.reads, News.class_id).\
##                        filter(News.class_id == 1).\
##                        order_by(desc(News.reads))[:data_limit]
##
##negativ_news = db.query(News.news_id, News.reads, News.class_id).\
##                        filter(News.class_id == -1).\
##                        order_by((News.reads))[:data_limit]
##
##news = pozitive_news + negativ_news
#news = db.query(News.news_id, News.reads, News.class_id).filter(News.class_id != 0)[:]
news = [(n.news_id, n.reads, n.class_id) for n in testData.getData(data_limit)]
news = dict([(i[0], (i[1], i[2])) for i in news])

print "Novic:", len(news)
# ==== ==== ==== ====
# Nov graf
H=nx.Graph()

# Nodes
for n in news:
    H.add_node(n)

# Povezave
edgewidth = []

for n in news:
    print "Edge:", n
    results = db.query(KernelResults).filter(KernelResults.news_id_1 == n).\
                                        filter(KernelResults.kernel_id == kernel_id).\
                                        filter(KernelResults.similarity > threshold).\
                                        order_by(desc(KernelResults.similarity))[:10]
    #print " -- ", len(results)
    for r in results:
        if H.has_node(r.news_id_2):
            H.add_edge(r.news_id_1, r.news_id_2)
            edgewidth.append(r.similarity * 4)

# ==== ==== ==== ====
# draw with matplotlib/pylab
import matplotlib.pyplot as plt
plt.figure(figsize=(58,58))
# with nodes colored by degree sized by population
node_color = [] # [nodes[n][1] + 1 for n in nodes] #r.news.class_id +
Hposition = {} # dict([(v, (randint(0, 100), randint(0, 100))) for v in H])

foo = 200000


def p2(centerX, centerY, radij):
    x = randint(-100, 100) / 100.0
    y = randint(-100, 100) / 100.0
    yitter = 10000
    return (centerX + radij * sin(x) + randint(-yitter, yitter), centerY + radij * cos(x) + randint(-yitter, yitter))
# v3

radij = 12000

for n in H.nodes():
    if news[n][1] == -1:
        node_color.append(2)
        Hposition[n] = p2(foo, foo, radij * 3)
    elif news[n][1] == 0:
        node_color.append(0)
        Hposition[n] = p2(foo, radij * 2)
    elif news[n][1] == 1:
        node_color.append(1)
        Hposition[n] = p2(foo, foo - 40000, radij * 3)

node_size = [math.log(news[n][0], 2) ** 3 for n in H.nodes()]
##
##nx.draw(H,Hposition,
##        width=edgewidth,
##     node_size=node_size,
##     node_color=node_color,
##     with_labels=True)

nx.draw_networkx_edges(H,Hposition,alpha=0.4,width=edgewidth, edge_color='m')
#nodesize=[wins[v]*50 for v in H]
nx.draw_networkx_nodes(H,Hposition,node_size=node_size,node_color='w',alpha=0.5)
#nx.draw_networkx_edges(H,Hposition,alpha=0.1,node_size=0,width=6,edge_color='k')
nx.draw_networkx_labels(H,Hposition,fontsize=20)

plt.savefig("grafi/NewsConnectionsGraph_size_%s_kernel_%s_threshold_%s.png" % (data_limit, kernel_id, threshold))
